package com.ts.animal.service.impl;

import com.google.common.collect.Lists;
import com.ts.animal.constant.AnimalConstant;
import com.ts.animal.dao.ExBaseStationDao;
import com.ts.animal.dao.ExRfidSyncDataDao;
import com.ts.animal.dao.RfidSyncDataDao;
import com.ts.animal.domain.RfidSyncDataDO;
import com.ts.animal.service.RfidSyncDataService;
import com.ts.common.utils.*;
import com.ts.system.util.SystemUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class RfidSyncDataServiceImpl implements RfidSyncDataService {
    @Autowired
    private RfidSyncDataDao rfidSyncDataDao;
    @Autowired
    private ExBaseStationDao exBaseStationDao;
    @Autowired
    private ExRfidSyncDataDao exRfidSyncDataDao;


    @Override
    public RfidSyncDataDO get(Long id) {
        return rfidSyncDataDao.get(id);
    }

    @Override
    public List<Map<String, Object>> list(Map<String, Object> map) {
        String testUserNames[] = SystemUtil.getSystemParams().get(AnimalConstant.key_name_test_user_name).getParamValue().split(",");
        if (Lists.newArrayList(testUserNames).contains(ShiroUtils.getUser().getUsername())) {
            map.put(AnimalConstant.test_user_name, AnimalConstant.test_user_name);
            map.put(AnimalConstant.test_user_deptId, ShiroUtils.getUser().getDeptId());
        } else {
            map.putAll(DataPerUitl.deptPer("bbs.dept_id"));
        }

        boolean checkLabelSwitch = AnimalConstant.open.equals(SystemUtil.getSystemParams().get(AnimalConstant.key_name_check_label_switch).getParamValue());
        if (checkLabelSwitch) {
            map.put(AnimalConstant.open_label_switch, AnimalConstant.open);
        }
        return exRfidSyncDataDao.list(map);
    }

    @Override
    public List<RfidSyncDataDO> queryBaseStationData(Map<String, Object> map) {
        boolean checkLabelSwitch = AnimalConstant.open.equals(SystemUtil.getSystemParams().get(AnimalConstant.key_name_check_label_switch).getParamValue());
        if (checkLabelSwitch) {
            map.put(AnimalConstant.open_label_switch, AnimalConstant.open);
        }
        return exBaseStationDao.rfidSyncDataList(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        map.putAll(DataPerUitl.deptPer("bbs.dept_id"));
        return exRfidSyncDataDao.count(map);
    }

    @Override
    public int save(Result result, RfidSyncDataDO rfidSyncData) {
        int ret = rfidSyncDataDao.save(rfidSyncData);
        if (ret == 0) {
            result.set(Result.FAIL, "保存失败", null);
        }
        return ret;


    }

    @Override
    public int update(Result result, RfidSyncDataDO rfidSyncData) {
        int ret = rfidSyncDataDao.update(rfidSyncData);
        if (ret == 0) {
            result.set(Result.FAIL, "更新失败", null);
        }
        return ret;
    }

    @Override
    public int remove(Result result, Long id) {
        int ret = rfidSyncDataDao.remove(id);
        if (ret == 0) {
            result.set(Result.FAIL, "删除失败", null);
        }
        return ret;
    }

    @Override
    public int batchRemove(Result result, Long[] ids) {
        int ret = rfidSyncDataDao.batchRemove(ids);
        if (ret != ids.length) {
            result.set(Result.FAIL, "部分删除失败", null);
        }
        return ret;
    }

    @Override
    public List<Object> temperatureChanges(String num) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("num", num);
        map.put("startCollectionTime", DateUtils.format(UtilDate.getDateBefore(new Date(), 7), DateUtils.DATE_TIME_PATTERN));
        List<RfidSyncDataDO> list = exBaseStationDao.rfidSyncDataList(map);
        List<Object> retList = new ArrayList<>();
        for (RfidSyncDataDO rfidSyncDataDO : list) {
            rfidSyncDataDO.getBodyTemperature();
            Map<String, Object> map1 = new HashMap<String, Object>();
            List<Object> array = new ArrayList<>();
            array.add(rfidSyncDataDO.getCollectionTime());
            array.add(rfidSyncDataDO.getBodyTemperature());
            map1.put("name", rfidSyncDataDO.getCollectionTime());
            map1.put("value", array);
            retList.add(map1);
        }
        return retList;
    }

}
